package cn.tedu.store.admin.webapi.controller;

import cn.tedu.store.commons.pojo.admin.dto.RoleAddNewDTO;
import cn.tedu.store.commons.pojo.admin.vo.RoleListItemVO;
import cn.tedu.store.commons.web.JsonResult;
import cn.tedu.store.admin.service.IRoleService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/role")
@Validated
@Api(tags = "2.角色管理模块")
public class RoleController {

    @Autowired
    IRoleService roleService;

    @PostMapping("/addNew")
    @ApiOperationSupport(order = 100)
    @ApiOperation("添加角色")
    public JsonResult addNew(@Validated RoleAddNewDTO roleAddNewDTO){
        roleService.addNew(roleAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除角色")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@PathVariable Long id){
        roleService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改角色详情")
    @ApiOperationSupport(order = 300)
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult updateById(@PathVariable Long id, RoleAddNewDTO roleAddNewDTO){
        log.debug("即将处理修改角色业务,参数id: {}, 角色信息: {}", id, roleAddNewDTO);
        roleService.updateById(id, roleAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("查询角色列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("")
    public JsonResult<List<RoleListItemVO>> list(){
        log.debug("开始处理获取管理员列表的请求 参数:无");
        List<RoleListItemVO> list = roleService.list();
        log.debug("查询完成");
        return JsonResult.ok(list);
    }

}
